package com.sinog.biz.schedule.async;

import com.alibaba.fastjson.JSON;
import com.sinog.biz.config.ThreadPoolConfig;
import com.sinog.biz.mapper.havexml.AzbjDatabaseInfoMapper;
import com.sinog.biz.mapper.havexml.DepartmentMapper;
import com.sinog.biz.mapper.havexml.JytjMapper;
import com.sinog.biz.mapper.havexml.NoticeInfoManageMapper;
import com.sinog.biz.mapper.havexml.ReportInfoManageMapper;
import com.sinog.biz.mapper.havexml.ZcwjMapper;
import com.sinog.biz.service.business.AzbjRsService;
import com.sinog.biz.service.census.AllCensusService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.core.domain.AzbjDatabaseInfo;
import com.sinog.core.domain.AzbjRsVo;
import com.sinog.core.domain.Jytj;
import com.sinog.core.domain.NoticeInfoManage;
import com.sinog.core.domain.ReplyInfo;
import com.sinog.core.domain.ReportInfoManage;
import com.sinog.core.domain.Zcwj;
import com.sinog.core.model.AzbjZlkSfb;
import com.sinog.core.model.JytjSfb;
import com.sinog.core.model.NoticeInfoSfb;
import com.sinog.core.model.ReportInfoSfb;
import com.sinog.core.model.ZcwjSfb;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @desc 异步任务类
 * @author wangxj
 * @date 2021-03-04 16:34
 */
@Component
@Slf4j
public class AsyncSql {

    @Autowired
    private AzbjRsService azbjRsService;
    @Autowired
    @Qualifier("bujiJdbcTemplate")
    private JdbcTemplate bujiJdbcTemplate;
    @Autowired
    private NoticeInfoManageMapper noticeInfoManageMapper;
    @Autowired
    private JytjMapper jytjMapper;
    @Autowired
    private AzbjDatabaseInfoMapper azbjDatabaseInfoMapper;
    @Autowired
    private ZcwjMapper zcwjMapper;
    @Autowired
    private ReportInfoManageMapper reportInfoManageMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private AllCensusService allCensusService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    private static List<String> levelList = new ArrayList<>(16);
    @Autowired
    private ThreadPoolConfig threadPoolConfig;

    static {
        levelList.add(Constants.OrgLevel.LEVEL_SFT);
        levelList.add(Constants.OrgLevel.LEVEL_QXSFJ);
        levelList.add(Constants.OrgLevel.LEVEL_DSSFJ);
        levelList.add(Constants.OrgLevel.LEVEL_JYJ);
    }

    /**
     * 司法厅---全部帮教对象（帮教对象人数）
     */
    @Async
    public void sftbjdxrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.sftbjdxrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 地市司法局---全部帮教对象（帮教对象人数）
     */
    @Async
    public void dssfjbjdxrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.dssfjbjdxrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 区县司法局---帮教对象人数
     */
    @Async
    public void qxsfjbjdxrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.qxsfjbjdxrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 司法厅---重点帮教对象数量
     */
    @Async
    public void sftzdbjdxrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.sftzdbjdxrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 地市司法局---重点帮教对象数量
     */
    @Async
    public void dssfjzdbjdxrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.dssfjzdbjdxrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 区县司法局---重点帮教对象数量
     */
    @Async
    public void qxsfjzdbjdxrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.qxsfjzdbjdxrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 司法厅--未衔接人数
     */
    @Async
    public void sftwxjrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.sftwxjrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 地市司法局--未衔接人数
     */
    @Async
    public void dssfjwxjrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.dssfjwxjrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 街道--未衔接人数
     */
    @Async
    public void qxsfjwxjrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.qxsfjwxjrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 街道--未衔接人数
     */
    @Async
    public void jdwxjrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.jdwxjrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 司法厅 -失联人数
     */
    @Async
    public void sftslrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.sftslrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 地市司法局 -失联人数
     */
    @Async
    public void dssfjslrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.dssfjslrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 区县司法局 -失联人数
     */
    @Async
    public void qxsfjslrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.qxsfjslrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 街道 -失联人数
     */
    @Async
    public void slrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.slrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }else {
            //如果没查出来，但是之前操作过，然后删除了，需要给他更新，置为0

        }
    }

    /**
     * 监狱-待衔接重点帮教对象人数
     */
    @Async
    public void dxjzddxrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.dxjzddxrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 监狱-待衔接人数
     */
    @Async
    public void dxjrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.dxjrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 监狱-监狱未衔接人数
     */
    @Async
    public void jywxjrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.jywxjrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 监狱-核查失败人数
     */
    @Async
    public void hcsbrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.hcsbrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 监狱-未进行出监、危险性评估人数
     */
    @Async
    public void wpgrs() {
        // 监狱-未进行出监、危险性评估人数
        List<AzbjRsVo> azbjRsVos = allCensusService.wpgrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 监狱-待核查人数
     */
    @Async
    public void dhcrs() {
        List<AzbjRsVo> azbjRsVos = allCensusService.dhcrs();
        if(null != azbjRsVos && !azbjRsVos.isEmpty()) {
            azbjRsService.updateRs(JsonUtil.toText(azbjRsVos));
        }
    }

    /**
     * 同步公告同步
     */
    @Async
    public void findTzgg() {
        String localCode = CodeUrlUtils.getLocalCode();
        //通知公告
        RowMapper<NoticeInfoSfb> rowMapper = new BeanPropertyRowMapper<>(NoticeInfoSfb.class);
        List<NoticeInfoSfb> tzggs = bujiJdbcTemplate.query("SELECT * " + "FROM" + " xtjc_tzgg_sfb " + " WHERE" + " state IN ('1','2','3','4') " + "AND adddate <= NOW()",rowMapper);
        for(NoticeInfoSfb tzgg : tzggs) {
            NoticeInfoManage noticeInfoManage = noticeInfoManageMapper.selectById(tzgg.getId());
            if(tzgg.getProid().contains(localCode)) {
                if(StringUtils.equals(tzgg.getState(),Constants.NumberStr.NUM_STR_ONE) || StringUtils.equals(tzgg.getState(),Constants.NumberStr.NUM_STR_TWO) || StringUtils.equals(tzgg.getState(),Constants.NumberStr.NUM_STR_FOUR)) {
                    if(null != noticeInfoManage) {
                        noticeInfoManage.setId(tzgg.getId());
                        noticeInfoManage.setTitle(tzgg.getTitle());
                        noticeInfoManage.setUsertype(tzgg.getUsertype());
                        noticeInfoManage.setAdddate(tzgg.getAdddate());
                        noticeInfoManage.setAddgovid(tzgg.getAddgovid());
                        noticeInfoManage.setAnnounce(tzgg.getAnnounce());
                        noticeInfoManage.setCColor(tzgg.getCColor());
                        noticeInfoManage.setCreateUser(tzgg.getCreateUser());
                        noticeInfoManage.setEnddate(tzgg.getEnddate());
                        noticeInfoManage.setFabulevel(tzgg.getFabulevel());
                        noticeInfoManage.updateById();
                    } else {
                        NoticeInfoManage noticeInfoManage1 = new NoticeInfoManage();
                        noticeInfoManage1.setId(tzgg.getId());
                        noticeInfoManage1.setTitle(tzgg.getTitle());
                        noticeInfoManage1.setUsertype(tzgg.getUsertype());
                        noticeInfoManage1.setAdddate(tzgg.getAdddate());
                        noticeInfoManage1.setAddgovid(tzgg.getAddgovid());
                        noticeInfoManage1.setAnnounce(tzgg.getAnnounce());
                        noticeInfoManage1.setCColor(tzgg.getCColor());
                        noticeInfoManage1.setCreateUser(tzgg.getCreateUser());
                        noticeInfoManage1.setEnddate(tzgg.getEnddate());
                        noticeInfoManage1.setFabulevel(tzgg.getFabulevel());
                        noticeInfoManage1.insert();
                    }
                } else if(StringUtils.equals(tzgg.getState(),Constants.NumberStr.NUM_STR_THREE)) {
                    if(null != noticeInfoManage) {
                        noticeInfoManage.deleteById();
                    }
                }
            }
        }
    }

    /**
     * 同步经验推介
     */
    @Async
    public void findJytj() {
        String localCode = CodeUrlUtils.getLocalCode();
        //通知公告
        RowMapper<JytjSfb> rowMapper = new BeanPropertyRowMapper<>(JytjSfb.class);
        //经验推介
        List<JytjSfb> jytjs = bujiJdbcTemplate.query("SELECT  * " + "FROM xtjc_jytj_sfb " + "WHERE state IN ('1','2','3','4') " + "AND addTime <= NOW();",rowMapper);
        for(JytjSfb jytj : jytjs) {
            Jytj jytj1 = jytjMapper.selectById(jytj.getId());
            if(jytj.getProid().contains(localCode)) {
                if(StringUtils.equals(jytj.getState(),Constants.NumberStr.NUM_STR_ONE) || StringUtils.equals(jytj.getState(),Constants.NumberStr.NUM_STR_TWO) || StringUtils.equals(jytj.getState(),Constants.NumberStr.NUM_STR_FOUR)) {
                    if(null != jytj1) {
                        jytj1.setId(jytj.getId());
                        jytj1.setTitle(jytj.getTitle());
                        jytj1.setUsertype(jytj.getUsertype());
                        String sfb = "中华人民共和国司法部法治局";
                        jytj1.setAdduserid(sfb);
                        jytj1.setCreateTime(jytj.getAddtime());
                        jytj1.setEndtime(jytj.getEndtime());
                        jytj1.setCreateUser(jytj.getCreateUser());
                        jytj1.setFabustate(jytj.getFabustate());
                        //经验推介插入下级机构
                        jytj1.setUpuserid(jytj.getProid());
                        jytj1.setFabutime(jytj.getAddtime());
                        jytj1.updateById();
                    } else {
                        Jytj jytj2 = new Jytj();
                        jytj2.setId(jytj.getId());
                        jytj2.setTitle(jytj.getTitle());
                        jytj2.setUsertype(jytj.getUsertype());
                        String sfb = "中华人民共和国司法部法治局";
                        jytj2.setAdduserid(sfb);
                        jytj2.setCreateTime(jytj.getAddtime());
                        jytj2.setEndtime(jytj.getEndtime());
                        jytj2.setCreateUser(jytj.getCreateUser());
                        jytj2.setFabustate(jytj.getFabustate());
                        //经验推介插入下级机构
                        jytj2.setUpuserid(jytj.getProid());
                        jytj2.setFabutime(jytj.getAddtime());
                        jytj2.insert();
                    }
                } else if(StringUtils.equals(jytj.getState(),Constants.NumberStr.NUM_STR_THREE)) {
                    if(null != jytj1) {
                        jytj1.deleteById(jytj.getId());
                    }
                }
            }
        }
    }

    /**
     * 同步资料卡
     */
    @Async
    public void findBjzl() {
        String localCode = CodeUrlUtils.getLocalCode();
        Date date = new Date();
        RowMapper<AzbjZlkSfb> rowMapper = new BeanPropertyRowMapper<>(AzbjZlkSfb.class);
        // todo 暂时先改成这样，这样是对的，有时间再看看 帮教资料
        //原来的   List<AzbjZlkSfb> zlks = bujiJdbcTemplate.query("SELECT * " + " FROM azbj_zlk_sfb " + "WHERE state IN ('1','2','3','4') " + "AND CREATE_TIME <= NOW();",rowMapper);
        List<AzbjZlkSfb> zlks = bujiJdbcTemplate.query("SELECT * " + " FROM azbj_zlk_sfb " + "WHERE state IN ('1','2','3','4') ;" ,rowMapper);
        for(AzbjZlkSfb zlk : zlks) {
            AzbjDatabaseInfo azbjDatabaseInfo = azbjDatabaseInfoMapper.selectById(zlk.getId());
            if(zlk.getProid().contains(localCode)) {
                if(StringUtils.equals(zlk.getState(),Constants.NumberStr.NUM_STR_ONE) || StringUtils.equals(zlk.getState(),Constants.NumberStr.NUM_STR_TWO) || StringUtils.equals(zlk.getState(),Constants.NumberStr.NUM_STR_FOUR)) {
                    if(null != azbjDatabaseInfo) {
                        azbjDatabaseInfo.setId(zlk.getId());
                        azbjDatabaseInfo.setType(zlk.getType());
                        azbjDatabaseInfo.setZlmc(zlk.getZlmc());
                        azbjDatabaseInfo.setUptime(zlk.getUptime());
                        azbjDatabaseInfo.setDownnum(zlk.getDownnum());
                        azbjDatabaseInfo.setCreateTime(zlk.getCreateTime());
                        String sfb = "中华人民共和国司法部法治局";
                        azbjDatabaseInfo.setAdduserid(sfb);
                        azbjDatabaseInfo.setCreateUser(zlk.getCreateUser());
                        azbjDatabaseInfo.setUpdateUser(zlk.getCreateUser());
                        azbjDatabaseInfo.setUpdateTime(date);
                        azbjDatabaseInfo.setUsertype(zlk.getUsertype());
                        azbjDatabaseInfo.setUpdanwei(zlk.getUpdanwei());
                        azbjDatabaseInfo.setFabustate(zlk.getFabustate());
                        azbjDatabaseInfo.updateById();
                    } else {
                        AzbjDatabaseInfo azbjDatabaseInfo1 = new AzbjDatabaseInfo();
                        azbjDatabaseInfo1.setId(zlk.getId());
                        azbjDatabaseInfo1.setType(zlk.getType());
                        azbjDatabaseInfo1.setZlmc(zlk.getZlmc());
                        azbjDatabaseInfo1.setUptime(zlk.getUptime());
                        azbjDatabaseInfo1.setDownnum(zlk.getDownnum());
                        azbjDatabaseInfo1.setCreateTime(zlk.getCreateTime());
                        String sfb = "中华人民共和国司法部法治局";
                        azbjDatabaseInfo1.setAdduserid(sfb);
                        azbjDatabaseInfo1.setCreateUser(zlk.getCreateUser());
                        azbjDatabaseInfo1.setUpdateUser(zlk.getCreateUser());
                        azbjDatabaseInfo1.setUpdateTime(date);
                        azbjDatabaseInfo1.setUsertype(zlk.getUsertype());
                        azbjDatabaseInfo1.setUpdanwei(zlk.getUpdanwei());
                        azbjDatabaseInfo1.setFabustate(zlk.getFabustate());
                        azbjDatabaseInfo1.insert();
                    }
                } else if(StringUtils.equals(zlk.getState(),Constants.NumberStr.NUM_STR_THREE)) {
                    if(null != azbjDatabaseInfo) {
                        azbjDatabaseInfo.deleteById(zlk.getId());
                    }
                }
            }
        }
    }

    /**
     * 同步政策文件
     */
    @Async
    public void findZcwj() {
        String localCode = CodeUrlUtils.getLocalCode();
        //自定义rowMapper
        RowMapper<ZcwjSfb> rowMapper = new BeanPropertyRowMapper<>(ZcwjSfb.class);
        //政策文件
        List<ZcwjSfb> zcwjs = bujiJdbcTemplate.query("SELECT *" + " FROM xtjc_zcwj_sfb " + "WHERE state IN ('1','2','3','4') " + "AND fabutime <= NOW();",rowMapper);
        for(ZcwjSfb zcwj : zcwjs) {
            Zcwj zcwj1 = zcwjMapper.selectById(zcwj.getId());
            if(zcwj.getProid().contains(localCode)) {
                if(StringUtils.equals(zcwj.getState(),Constants.NumberStr.NUM_STR_ONE) || StringUtils.equals(zcwj.getState(),Constants.NumberStr.NUM_STR_TWO) || StringUtils.equals(zcwj.getState(),Constants.NumberStr.NUM_STR_FOUR)) {
                    if(null != zcwj1) {
                        zcwj1.setId(zcwj.getId());
                        zcwj1.setTitle(zcwj.getTitle());
                        zcwj1.setUsertype(zcwj.getUsertype());
                        zcwj1.setGovid(zcwj.getGovid());
                        //将录入单位录入进去
                        zcwj1.setAddgovid(zcwj.getAddgovid());
                        zcwj1.setFabutime(zcwj.getFabutime());
                        zcwj1.setEndtime(zcwj.getEndtime());
                        zcwj1.setCreateUser(zcwj.getCreateUser());
                        zcwj1.setFabustate(zcwj.getFabustate());
                        zcwj1.updateById();
                    } else {
                        Zcwj zcwj2 = new Zcwj();
                        zcwj2.setId(zcwj.getId());
                        zcwj2.setTitle(zcwj.getTitle());
                        zcwj2.setUsertype(zcwj.getUsertype());
                        zcwj2.setGovid(zcwj.getGovid());
                        //将录入单位录入进去
                        zcwj2.setAddgovid(zcwj.getAddgovid());
                        zcwj2.setFabutime(zcwj.getFabutime());
                        zcwj2.setEndtime(zcwj.getEndtime());
                        zcwj2.setCreateUser(zcwj.getCreateUser());
                        zcwj2.setFabustate(zcwj.getFabustate());
                        zcwj2.insert();
                    }
                } else if(StringUtils.equals(zcwj.getState(),Constants.NumberStr.NUM_STR_THREE)) {
                    if(null != zcwj1) {
                        zcwj1.deleteById(zcwj.getId());
                    }
                }
            }
        }
    }

    /**
     * 同步上报协调
     */
    @Async
    public void findSbxt() {
        String localCode = CodeUrlUtils.getLocalCode();
        //自定义rowMapper
        RowMapper<ReportInfoSfb> rowMapper = new BeanPropertyRowMapper<>(ReportInfoSfb.class);
        //上报协调
        List<ReportInfoSfb> rimc = bujiJdbcTemplate.query("SELECT * " + " FROM xtjc_sxt_sfb" + " WHERE upload_flag = '2' " + "AND Replytime <= NOW();",rowMapper);
        for(ReportInfoSfb reportInfoManageCopy : rimc) {
            ReportInfoManage reportInfoManage = reportInfoManageMapper.selectById(reportInfoManageCopy.getId());
            //判断，upload_flag是否相等，如果相等再让修改，防止某一秒司法厅点击撤回
            String uploadFlag = reportInfoManageMapper.getUploadFlag(reportInfoManageCopy.getId());
            if(StringUtils.equals(reportInfoManageCopy.getUploadFlag(),uploadFlag) || StringUtils.equals(reportInfoManageCopy.getUploadFlag(),Constants.NumberStr.NUM_STR_TWO)) {
                ReplyInfo replyInfo = reportInfoManageMapper.getReplyInfoByContentId(reportInfoManageCopy.getContentId());
                if(reportInfoManageCopy.getCsjigou().contains(localCode)) {
                    if(null != reportInfoManage) {
                        reportInfoManage.setId(reportInfoManageCopy.getId());
                        reportInfoManage.setTitle(reportInfoManageCopy.getTitle());
                        reportInfoManage.setSbtype(reportInfoManageCopy.getSbtype());
                        reportInfoManage.setUpdanwei(reportInfoManageCopy.getUpdanwei());
                        reportInfoManage.setTipFlag(reportInfoManageCopy.getTipFlag());
                        reportInfoManage.setJglevel(reportInfoManageCopy.getJglevel());
                        reportInfoManage.setAddgovid(reportInfoManageCopy.getAddgovid());
                        reportInfoManage.setSendtime(reportInfoManageCopy.getSendtime());
                        reportInfoManage.setReadtime(reportInfoManageCopy.getReadtime());
                        reportInfoManage.setUploadFlag(reportInfoManageCopy.getUploadFlag());
                        reportInfoManage.insertOrUpdate();
                    }
                    if(null != replyInfo) {
                        reportInfoManageMapper.updateReply(reportInfoManageCopy.getContentId(),reportInfoManageCopy.getReplytime(),localCode,reportInfoManageCopy.getReplyFlag());
                    } else {
                        reportInfoManageMapper.insertReply(reportInfoManageCopy.getContentId(),reportInfoManageCopy.getReplytime(),localCode,reportInfoManageCopy.getReplyFlag());
                    }
                }
            } else if(StringUtils.equals(uploadFlag,Constants.NumberStr.NUM_STR_THREE)) {
                //修改部级upload状态为当前省级upload状态
                bujiJdbcTemplate.update("UPDATE xtjc_sxt SET upload_flag = ? WHERE id =?",uploadFlag,reportInfoManageCopy.getContentId());
                //判断司法厅是否存在回复信息，如果存在则删除之前的回复信息
                List<ReportInfoSfb> query = bujiJdbcTemplate.query(" SELECT id,content_id,Replytime,Csjigou FROM xtjc_shf WHERE content_id = ?",rowMapper,reportInfoManageCopy.getContentId());
                if(!query.isEmpty()) {
                    bujiJdbcTemplate.update("DELETE FROM xtjc_shf WHERE content_id = ?",reportInfoManageCopy.getContentId());
                    bujiJdbcTemplate.update("DELETE FROM xtjc_sxt_sfb WHERE content_id = ?",reportInfoManageCopy.getContentId());
                }
            }
        }
    }
    @Async
    public void census(String proCode) {
        CountDownLatch countDownLatch = new CountDownLatch(50);
        for(String level : levelList) {
            List<String> syDepartmentId = departmentMapper.getSyDepartmentId(level,proCode);
            try {
                if(syDepartmentId != null) {
                    for(String departmentId : syDepartmentId) {
                        log.info("level:{}开始执行>>>>>>>>>>",level);
                        threadPoolConfig.taskExecutor().submit(()->{
                            log.info("线程{},开始执行>>>>>>>>>>>",Thread.currentThread().getName());
                            if(departmentId != null) {
                                //获取八大指标结果
                                String result = allCensusService.getBdStatisticalReports(departmentId,level,Constants.NumberStr.NUM_STR_FU_ONE,Constants.NumberStr.NUM_STR_FU_ONE);
                                //转化为map
                                Map<String,Object> map = JsonUtil.toMap(result);
                                //获取map中的data数据
                                List<Map<String,String>> data = (List<Map<String,String>>)map.get("data");
                                //遍历每条数据，放入redis中
                                for(Map<String,String> m1 : data) {
                                    if(null == m1) {
                                        redisTemplate.opsForHash().put(departmentId,m1.get(Constants.Census.CHILDS),"0");
                                    }else {
                                        redisTemplate.opsForHash().put(departmentId,m1.get(Constants.Census.CHILDS),JSON.toJSONString(m1));
                                    }
                                }
                            }
                        });
                    }
                }
            } catch(Exception e) {
                throw new RuntimeException(e);
            } finally {
                countDownLatch.countDown();
            }
        }
        try {
            countDownLatch.await();
        } catch(InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("++++++++++++census++++++++++++++++countdown执行完毕");
    }

    /**
     * 统计分析报表
     * @param proCode 省code
     */
    @Async
    public void bbCensus(String proCode) {
        CountDownLatch countDownLatch = new CountDownLatch(50);
        for(String level : levelList) {
            List<String> syDepartmentId = departmentMapper.getSyDepartmentId(level,proCode);
            try {
                if(syDepartmentId != null) {
                    for(String departmentId : syDepartmentId) {
                        log.info("level:{}开始执行>>>>>>>>>>",level);
                        threadPoolConfig.taskExecutor().submit(()->{
                            log.info("线程{},开始执行>>>>>>>>>>>",Thread.currentThread().getName());
                            if(departmentId != null) {
                                //获取八大指标结果
                                String result = allCensusService.getStatisticalReports(departmentId,level,Constants.NumberStr.NUM_STR_FU_ONE,Constants.NumberStr.NUM_STR_FU_ONE);
                                //转化为map
                                Map<String,Object> map = JsonUtil.toMap(result);
                                //获取map中的data数据
                                List<Map<String,String>> data = (List<Map<String,String>>)map.get("data");
                                //遍历每条数据，放入redis中
                                for(Map<String,String> m1 : data) {
                                    if(null == m1) {
                                        redisTemplate.opsForHash().put("bb"+departmentId,m1.get(Constants.Census.CHILDS),"0");
                                    }else {
                                        redisTemplate.opsForHash().put("bb"+departmentId,m1.get(Constants.Census.CHILDS),JSON.toJSONString(m1));
                                    }
                                }
                            }
                        });
                    }
                }
            } catch(Exception e) {
                throw new RuntimeException(e);
            } finally {
                countDownLatch.countDown();
            }
        }
        try {
            countDownLatch.await();
        } catch(InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("++++++++++++bbCensus++++++++++++++++countdown执行完毕");
    }
}